const arr = [1, 2, 3, 4, [5, 6], [7, [8, 9]]]

Array.prototype.myFlat = function (depth = 1) {
 const list = []
  for(const item of this){
    if(depth && Array.isArray(item)){
      list.push(...item.myFlat(depth - 1))
    }else{
      list.push(item)
    }
  }
  return list
};
const result = arr.myFlat()
console.log(result)
